package de.jare.photoswapper;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.os.Bundle;
import android.provider.Settings.System;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;


public class photoswapper extends Activity implements SurfaceHolder.Callback {
    
    private Camera mCamera;
    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
    HttpHandler h = new HttpHandler();

    /* What's this ? */
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }

    /* ToDo: Documentation */
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case R.id.quit:
                finish();
                return true;
        case R.id.about:
        		Intent i = new Intent ( this, About.class);
        		startActivity(i);
        		break;
        case R.id.get:
    		Intent e = new Intent ( this, Get.class);
    		startActivity(e);
    		break;
        }
        return false;
    } 
    
    /* Constructor of this action */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Hide the window title.
        getWindow().setFormat(PixelFormat.TRANSLUCENT);

        setContentView(R.layout.main);
        mSurfaceView = (SurfaceView)findViewById(R.id.surface);
        mSurfaceHolder = mSurfaceView.getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        
        
        Button bt3 = (Button)findViewById(R.id.take_imagebt);
        bt3.setOnClickListener(getImgListener);
        
        // assign android id to HttpHandler instance
        h.AndroidID = System.getString(this.getContentResolver(), System.ANDROID_ID);
    }
    
    View.OnClickListener getImgListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        	mCamera.takePicture(null, null, jpegCallback);
        }
    };
    
    
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return super.onKeyDown(keyCode, event);
        }
 
        if (keyCode == KeyEvent.KEYCODE_SPACE) {
        	Log.d("PS", "photoswapper.onKeyDown(): takePicture()...");
            mCamera.takePicture(null, null, jpegCallback);
            return true;
        }
        return false;
    }
    
    /* Callback of mCamera.takePicture(null, null, jpegCallback); */
    PictureCallback jpegCallback = new PictureCallback() {
           public void onPictureTaken(byte[] data, Camera c) { 
        	   Log.d("PS", "photoswapper.jpegCallback: PictureCallback()...");
        	   
        	   // Upload byte array by HttpHandler
        	   h.UploadPicture(data);
        	   
        	   // startPreview back again
        	   mCamera.startPreview();
           }
     };

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
    	
        mCamera = Camera.open();
        Camera.Parameters parameters = mCamera.getParameters();
    	parameters.setPictureFormat(PixelFormat.JPEG);
    	parameters.setPictureSize(640, 480);
    	mCamera.setParameters(parameters);
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mCamera.stopPreview();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);
        try {
			mCamera.setPreviewDisplay(holder);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        mCamera.startPreview();
    }

}